package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import dto.ClueDto;
import dto.MarkFalseClueDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;

    @PostMapping
    public Result addClue(@RequestBody Clue clue) {
        log.info("新增线索: {}", clue);
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());

        clueService.save(clue);
        return Result.success();
    }
    @GetMapping
    public Result list(ClueDto clueDto){
        log.info("查询线索列表: {}", clueDto);
        PageResult<Clue> pageResult = clueService.listClues(clueDto);
        return Result.success(pageResult);
    }
    /**
     * 分配线索
     *
     */
   @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索: {} {}", clueId , userId);
        Clue clue = new Clue();
        clue.setId( clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }
    /**
     * 跟进线索
     *
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询线索id为{}",id);
        Clue clue = clueService.getClueById(id);
        return Result.success(clue);
    }
    @PutMapping
    public Result updateClue(@RequestBody Clue clue) {
        log.info("更新线索: {}", clue);
        clueService.trackClue(clue);
        return Result.success();

    }
    /**
     * 伪线索
     *
     */
    @PutMapping("/false/{id}")
    public Result markFalseClue(@PathVariable Integer id,  @RequestBody MarkFalseClueDto markFalseClueDto) {
        log.info("伪线索: {} {}", id , markFalseClueDto);
        clueService.markFalseClue(id,markFalseClueDto);
        return Result.success();


    }
    /**
     * 转商机
     *
     */
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("转商机: {}", id);
        clueService.toBusiness(id);
        return Result.success();


    }
    /**
     * 线索池查询
     *
     */
    @GetMapping("/pool")
    public Result listPool(ClueDto clueDto) {
        log.info("查询线索池列表:{}", clueDto);
        PageResult<Clue> cluePage = clueService.poolList(clueDto);
        return Result.success(cluePage);
    }






}




